package com.soft.knowledge.controller;

import com.soft.knowledge.req.EbookByCategoryReq;
import com.soft.knowledge.req.EbookQueryReq;
import com.soft.knowledge.req.EbookSaveReq;
import com.soft.knowledge.resp.CommonResp;
import com.soft.knowledge.resp.EbookQueryResp;
import com.soft.knowledge.resp.PageResp;
import com.soft.knowledge.service.EbookService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 电子书(Ebook)表控制层
 *
 * @author makejava
 * @since 2022-11-19 13:17:14
 */
@RestController
@RequestMapping("ebook")
public class EbookController {
    /**
     * 服务对象
     */
    @Resource
    private EbookService ebookService;

    /**
     * 通过请求参数列表name属性进行模糊查询
     *
     * @param req 请求参数列表
     * @return 多条数据
     */
    @GetMapping("/list")
    public CommonResp list(@Valid EbookQueryReq req){
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> list = ebookService.list(req);
        resp.setContent(list);
        resp.setMessage("查询成功！");
        return resp;
    }
    @PostMapping("/save")
    public CommonResp save(@RequestBody @Valid EbookSaveReq req){//post json方式请求要加@RequestBody，form表单方式提交不需要加该注解
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        resp.setMessage("保存成功");
        return resp;
    }
    //restful
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp = new CommonResp<>();
        ebookService.delete(id);
        return resp;
    }
    @GetMapping("/selectByCategory")
    public CommonResp selectByCategory(EbookByCategoryReq req){
        CommonResp<List<EbookQueryResp>> resp = new CommonResp<>();
        resp.setContent(ebookService.selectByCategory(req));

        return resp;
    }
}